#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#include "sqlite3.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loadsql();
}

MainWindow::~MainWindow()
{
    delete ui;
}
QString int2str(int i ){
    QString temp;
    switch(i){
    case 0:
        temp = "0";break;
    case 1:
        temp = "a";break;
    case 2:
        temp = "b";break;
    case 3:
        temp = "c";break;
    case 4:
        temp = "d";break;
    case 5:
        temp = "e";break;
    case 6:
        temp = "f";break;
    case 7:
        temp = "g";break;
    case 8:
        temp = "h";break;
    case 9:
        temp = "i";break;
    case 10:
        temp = "j";break;
    case 11:
        temp = "k";break;
    case 12:
        temp = "l";break;
    case 13:
        temp = "m";break;
    case 14:
        temp = "n";break;
    case 15:
        temp = "o";break;
    case 16:
        temp = "p";break;
    case 17:
        temp = "q";break;
    case 18:
        temp = "r";break;
    case 19:
        temp = "s";break;
    case 20:
        temp = "t";break;
    case 21:
        temp = "u";break;
    case 22:
        temp = "v";break;
    case 23:
        temp = "w";break;
    case 24:
        temp = "x";break;
    case 25:
        temp = "y";break;
    case 26:
        temp = "z";break;
    case 27:
        temp = "\'";break;
    case 28:
        temp = ";";break;
    case 29:
        temp = "`";break;
    case 45:
        temp = "[";break;
    case 46:
        temp = "]";break;
    case 55:
        temp = ",";break;
    case 56:
        temp = ".";break;
    }
    return temp;
}
int callback(void *NotUsed, int argc, char **argv, char **azColName){

    QMap<QString,QStringList> *keyMap = (QMap<QString,QStringList> *)NotUsed;
    QString mlen,code,phrase;
    mlen=argv[0];
    code.clear();

    for(int i=1;i<=mlen.toInt();i++){
        if(argv[i] != NULL){
            QString temp;
            temp=argv[i];
            code.append(int2str(temp.toInt()));
        }
    }
    phrase=argv[6];
            if (!code.isEmpty()){
                QStringList tmp;
                if(keyMap->contains(phrase)){
                    tmp=keyMap->value(phrase);
                    tmp<<code;
                    keyMap->insert(phrase,tmp);
                }else{
                    tmp.append(code);
                    keyMap->insert(phrase,tmp);
                }
            }
    return 0;
}
void MainWindow::loadsql(){
    QDir q;
    //qDebug()<<"\n current Dir" <<q.currentPath() << "\n Home path" << q.homePath() << "\n root Path" << q.rootPath() << "\n";
    QString check = q.currentPath();
    QStringList checkList = check.split(QDir::separator());
    check = checkList.join(QDir::separator());
    check.append(QDir::separator() );
    check.append("boshiamy_t.db");
    sqlite3 *db ;
    char *err;
    if((sqlite3_open(check.toAscii(),&db)!=SQLITE_OK) && (!QFile::exists(check)) ){
        QMessageBox::about(this,"找不到輸入法表格","請於官網下載 iBus輸入法表格，並將安裝包中的 boshiamy_t.db放置於本程式的同一目錄下， 重新啟動。 ");
    }
    else{
        sqlite3_exec(db,"select mlen,m0,m1,m2,m3,m4,phrase from phrases",callback,(void *)&Standard_key,&err) ;
        sqlite3_close(db);
        //重排編碼。
        foreach(QString a , Standard_key.keys())
        {
            QStringList b = Standard_key.value(a);
            QMap<int,QStringList> map;
            foreach(QString c , b){
                QStringList tmp;
                if(!c.contains("[]",Qt::CaseInsensitive)){
                  if(map.contains(c.length())){
                      tmp = map.value(c.length());
                      tmp << c;
                      map.insert(c.length(),tmp);
                  }else{
                      tmp.append(c);
                      map.insert(c.length(),tmp);
                  }
                }
            }
            QStringList d;
            for (int i = 1; i <6 ; i++){
                if(map.contains(i)){
                    d.append(map.value(i));
                }
            }
            Standard_key.insert(a,d);
        }
    }
    if(Standard_key.isEmpty()){
        QMessageBox::about(this,"輸入法表格錯誤","請於官網下載 iBus輸入法表格，並將安裝包中的 boshiamy_t.db放置於本程式的同一目錄下， 重新啟動。 ");
        qApp->exit(0);
    }
}
void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
QString MainWindow::GetCode(QString str){
    QString temp = "";
    if (Standard_key.contains(str)){
        temp = Standard_key.value(str).first();
    }
    if(!temp.compare("")){
        temp = str;
    }
    return temp;
}
void MainWindow::on_lineEdit_textChanged(QString)
{
    if(ui->lineEdit->text().isEmpty()){
        ui->textBrowser->setText("");
    }
    else{
        QString str = ui->lineEdit->text();
        QStringList partstr;
        for(int i=0 ; i< str.length();i++){
            partstr.append(str.at(i));
        }
        QString output = "";
        foreach(QString a , partstr){
            if(GetCode(a)!=""){
                output.append(this->GetCode(a));
                output.append(" ");
            }
        }
        if(!output.isEmpty()){
            QClipboard *clipboard =  qApp->clipboard();
            clipboard->setText(output,QClipboard::Clipboard);
            //qDebug()<<output;
            this->ui->textBrowser->setText(output);
        }
    }
}

void MainWindow::on_action_triggered()
{
   qApp->exit(1);
}

void MainWindow::on_action_2_triggered()
{
    QMessageBox::about(this,"關於 嘸蝦米","嘸蝦米輸入法屬於行易公司所有。是好用、易學、實用、難忘的中文輸入法…用過之後你一定會愛上它！！ ");
}

void MainWindow::on_action_XDD_triggered()
{
    QMessageBox::about(this,"關於 XDD",",,x『逗點逗點ㄟ哭死』是嘸蝦米一個很棒的功能，它可以把剪貼簿裡的蝦米碼轉換成文字。而 XDD 『ㄟ哭死逗逗』則是反其道而行的惡搞小工具，可以將文字轉換成蝦米碼。可以用做即時通交談時情人間的防閃訊息、部落格裡的加密文章、或是想搞點神祕的日記書寫。有了XDD，就不用擔心忘了怎麼拆碼了。同時，XDD貼心的幫您把輸出的蝦米碼自動的加到剪貼簿中，您只要在發表的地方，選擇『貼上』就可以了。");
}
